package com.api.gbhg.liuwenbo.version3.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.api.AdministrationApi;
import com.api.gbhg.liuwenbo.version2.api.BudgetSectionIncomeApi2;
import com.api.gbhg.liuwenbo.version2.api.CauseApi;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * @Description 行政运行
 * @Version 3.0
 * @Author lwb
 * @Date 22.12.14
 */
@Path("v3/Administration")
public class AdministrationApi3 extends BudgetSectionIncomeApi2 {

    private AdministrationApi api = new AdministrationApi();

    @POST
    @Path("/getAdministrationList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAdministrationList(Map<String,Object> param){
        Map msgMap = new HashMap();
        if (param == null ||!param.containsKey("nf") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = api.paramFormat(param);

        List<BudgetSectionIncome2> czbksyData = null;//所有除缉私局
        List<BudgetSectionIncome2> czbkjsjData = null;//缉私局数据

        try {
            paramFormat.put("dw",getdw());
            czbksyData = CauseApi.AdjustDataStructure(new ArrayList<>(),api.getCzbksrData(paramFormat));
            paramFormat.put("dw",29);
            czbkjsjData = CauseApi.AdjustDataStructure(new ArrayList<>(),api.getCzbksrData(paramFormat));
        }catch (Exception e){
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("非缉私局",structureConversion(czbksyData,paramFormat.get("nf").toString()));
        map.put("缉私局",structureConversion(czbkjsjData,paramFormat.get("nf").toString()));

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }



    /**
     * 转结构 去下级化 带xj结构可用
     * @param paramList
     *          结果集
     * @param nf
     *          年份
     * @return
     */
    private List<BudgetSectionIncome2> structureConversion(List<BudgetSectionIncome2> paramList,String nf){
        List<BudgetSectionIncome2> list = new LinkedList<>();

        for (int i=0;i<paramList.size();i++){
            BudgetSectionIncome2 bean = paramList.get(i);
            bean.setCj(1);
            bean.setNf(nf);
            List<BudgetSectionIncome2> xjBean = bean.getXj();
            if (xjBean != null && xjBean.size()>0){
                for (int j = 0; j < xjBean.size(); j++) {
                    BudgetSectionIncome2 tempBean = xjBean.get(j);
                    xjChange(tempBean,2,bean.getId(),nf,list);
                }
            }
            if (xjBean!=null)//去xj化
                bean.setXj(null);

            list.add(bean);
        }
        return list;
    }

    /**
     * 下级结构转换
     * @param xjBean
     *          对象
     * @param cj
     *          层级
     * @param sjid
     *          上级id
     * @param nf
     *          年份
     * @param list
     *          目标集合
     */
    public void xjChange(BudgetSectionIncome2 xjBean,Integer cj,String sjid,String nf,List<BudgetSectionIncome2> list){
        xjBean.setCj(cj);
        xjBean.setSjid(sjid);
        xjBean.setNf(nf);
        if (xjBean.getXj() != null && xjBean.getXj().size()>0){
            for (int i = 0; i < xjBean.getXj().size(); i++) {
                BudgetSectionIncome2 tempBean = xjBean.getXj().get(i);
                xjChange(tempBean,cj+1,xjBean.getId(),nf,list);
            }
        }
        if (xjBean.getXj()!=null)//去xj化
            xjBean.setXj(null);

        list.add(xjBean);
    }

    public static String getdw(){
        RecordSet rs = new RecordSet();
        String sql = "select dlhsdw as 'dwid'  from uf_dlhsdwgl left join uf_dwsxpz " +
                "on uf_dlhsdwgl.dlhsdw=uf_dwsxpz.xzxtdw where dwxz=0 and dlhsdw !=29 ";
        rs.executeQuery(sql);
        List<String> list = new ArrayList<>();
        while (rs.next()){
            list.add(rs.getString("dwid"));
        }
        return String.join(",",list);
    }
}
